package com.distributer.timer.job;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.support.CronTrigger;

import com.distributer.timer.model.ScheduledTask;

public class ScheduledTaskJob implements Runnable {

	private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledTaskJob.class);
	
	private final ScheduledTask scheduledTask;
	
	private final String clientId;

	public ScheduledTaskJob(ScheduledTask scheduledTask,String clientId) {
		this.scheduledTask = scheduledTask;
		this.clientId = clientId;
	}

	@Override
	public void run() {
		if (!scheduledTask.isStop()) {
			LOGGER.info(clientId+" 客户端执行了任务！");
			//HttpUtils.cs("utf-8").get(scheduledTask.getRequestUrl());
		}
	}

	public Trigger getTrigger() {
		Trigger trigger = new Trigger() {
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				CronTrigger cronTrigger = new CronTrigger(scheduledTask.getTaskCron());
				return cronTrigger.nextExecutionTime(triggerContext);
			}
		};
		return trigger;
	}

	public ScheduledTask getScheduledTask() {
		return scheduledTask;
	}

	public String getClientId() {
		return clientId;
	}
}
